package qj.tool.gae.dump;

import java.io.InputStream;
import java.net.HttpURLConnection;

import qj.tool.gae.db.AEDB;
import qj.tool.http.WebSession;
import qj.util.ZipUtil;
import qj.util.funct.P1;

public class DumpConnector {

	static WebSession webSession;
	public String host;
	public DumpConnector(String host) {
		this.host = host;
		webSession = new WebSession();
		webSession.connDecor = new P1<HttpURLConnection>() {public void e(HttpURLConnection conn) {
			conn.addRequestProperty("Client", "AAhhaa");
		}};
	}

	public String getDumpPlan(String profile) {
		return webSession.getString("http://" + host + "/open?module=dump&action=plan&profile=" + profile);
	}

	public String getJsonDump(String type,String ids) {
		return webSession.post("http://" + host + "/open?module=dump&action=json&type=" + type,ids.getBytes(),"image/png");
	}
	public InputStream getBlobDump(String type, String id, String field) {
		try {
			return webSession.getStream("http://" + host + "/open?module=dump&action=blob&type=" + type + "&id=" + AEDB.asciiKeyString(id) + "&field=" + field);
		} catch (Exception e) {
			return null;
		}
	}

	public void startUpload(String profile) {
		webSession.getString("http://" + host + "/open?module=dump&action=startUpload&profile=" + profile);
	}

	public void uploadJson(String type, String json) {
		webSession.post("http://" + host + "/open?module=dump&action=uploadJson&type=" + type,ZipUtil.gzip(json),"image/png");
	}

	public void uploadBlob(Object id, String type, String field, byte[] content) {
		webSession.post("http://" + host + "/open?module=dump&action=uploadBlob&type=" + type + "&field=" + field + "&id=" + id,content,"image/png");
	}

}
